/**
 * 基于vant插件的弹窗交互工具类
 */
import Vue from 'vue'
// import Toast from 'path/to/@vant/weapp/dist/toast/toast'
import { Toast, Notify, Dialog } from 'vant'
// import Notify from 'path/to/@vant/weapp/dist/notify/notify'
import validate from '@/utils/open/validate'

// 弹窗组件
// import {
//   ToastPlugin,
//   TransferDom,
//   XDialog,
//   ConfirmPlugin, AlertPlugin,
//   DatetimePlugin, LoadingPlugin,
//   Actionsheet } from 'vux'

// Vue.use(ToastPlugin, { time: '2500' })
// Vue.use(ConfirmPlugin)
// Vue.use(AlertPlugin)
// Vue.use(DatetimePlugin)
// Vue.use(LoadingPlugin)
// Vue.use(Actionsheet)
// Vue.use(ToastPlugin)
// Vue.use(XDialog)
// Vue.directive('transfer-dom', TransferDom)

export default {
  loading (msg) {
    // 显示
    Vue.$vux.loading.show({
      text: msg
    })
  },
  hideLoading () {
    // 隐藏
    Vue.$vux.loading.hide()
  },
  confirm (title, content, fn) {
    Dialog.confirm({
      title: title,
      message: content
    }).then(() => {
      fn(true)
      // on confirm
    }).catch(() => {
      fn(false)
      // on cancel
    })
    // Vue.$vux.confirm.show({
    //   title: title || '提示',
    //   content: content,
    //   onCancel() {
    //     fn && fn(false)
    //     // Vue.$vux.confirmDom.style.display = 'none'
    //   },
    //   onConfirm() {
    //     fn && fn(true)
    //     // Vue.$vux.confirmDom.style.display = 'none'
    //   }
    // })
  },
  /**
   * toast
   * @param {String} msg
   * @param {String} pos 显示位置，可选值 default, top, middle, bottom
   */
  toast (msg, pos) {
    if (!validate.isNull(msg)) {
      pos = pos || 'top'
      Toast(msg, pos)
    }
  },
  /**
   * 隐藏 toast
   */
  hideToast () {
    Vue.$vux.toast.hide()
  },
  /**
   * 成功提示
   * @param {String} msg
   */
  successMsg (msg) {
    Vue.$vux.toast.show({
      text: msg || '成功',
      type: 'success'
    })
  },
  /**
   * 取消提示
   * @param {String} msg
   */
  cancelMsg (msg) {
    Vue.$vux.toast.show({
      text: msg,
      type: 'cancel',
      time: 3000
    })
  },
  /**
   * 错误提示
   * @param {String} msg
   */
  errorMsg (msg) {
    Vue.$vux.alert.show({
      title: '提示',
      content: msg,
      onShow () {
        // console.log('Plugin: I\'m showing')
      },
      onHide () {
        // console.log('Plugin: I\'m hiding')
      }
    })
  },
  /**
   * 警告提示
   * @param {String} msg
   */
  warnMsg (msg) {
    // Vue.$vux.toast.text(msg, 'top')
    Vue.$vux.toast.show({
      text: msg,
      type: 'warn',
      time: 4000
    })
  },
  // 通知类型: 支持primary、success、warning、danger四种通知类型，默认为danger
  tip (type, msg) {
    const typeObj = {
      1: 'primary',
      2: 'success',
      3: 'danger',
      4: 'warning'
    }
    // console.log(type, msg, Notify)
    Notify({ type: typeObj[type], message: msg })
  }
}

